package listmailbox;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use strict;
use CGI::Carp qw(fatalsToBrowser);
use base 'func';

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub total_list_mailbox {
    my $self = shift;

    my @sqlu = ();
    if ( $self->{session}->{name} eq 'Admin' && $self->{p}->{username} ) {
        push @sqlu, $self->{p}->{username};

    }
    else { push @sqlu, $self->{session}->{user} }

    my $sth = $self->{dbh}->prepare(
        'SELECT domain
                         FROM domain 
                         WHERE idnane = ?
                         ORDER BY created
                             '
    );
    $sth->execute(@sqlu);

    my @domains = ();
    while ( my $href = $sth->fetchrow_hashref() ) {
        push( @domains, $href->{domain} );
    }
    $sth->finish();

    if ( defined( $self->{p}->{fDomain} ) ) {
        my ( $index, $index2, $found );

        for ( $index = 0 ; $index < @domains ; $index++ ) {

            if ( $domains[$index] eq $self->{p}->{fDomain} ) {
                $index2 = $index;
                $found  = 1;
                last;
            }
        }

        if ($found) {

            splice @domains, $index2, 1;

            my $i = $#domains + 1;
            $domains[$i] = $self->{p}->{fDomain};

        }
    }

    my @domains2 = map +{ domain => $_, domain2 => $_ }, @domains;

    my @cl0 = ();

    my @sqlb = ();
    my $url;

    if (   $self->{session}->{name} eq 'Admin'
        && $self->{p}->{domain}
        && $self->{p}->{username} )
    {

        push @cl0, $self->{p}->{username}, $self->{p}->{domain};
        $self->{p}->{url} =
            '&domain='
          . $self->{p}->{domain}
          . '&username='
          . $self->{p}->{username};

    }
    else {

        if ( defined( $self->{p}->{fDomain} ) ) {

            $self->{p}->{url} = '&domain=' . $self->{p}->{fDomain};
            push @cl0, $self->{session}->{user}, $self->{p}->{fDomain};
        }
        elsif ( defined( $self->{p}->{domain} ) ) {
            $self->{p}->{url} = '&domain=' . $self->{p}->{domain};

            push @cl0, $self->{session}->{user}, $self->{p}->{domain};
        }
        else {
            $self->{p}->{url} = '&domain=' . $domains[$#domains];

            push @cl0, $self->{session}->{user}, $domains[$#domains];
        }
    }

    my $sth = $self->{dbh}->prepare(
        "SELECT count(*)
                         FROM alias AS t1,
                              domain AS t2
                         WHERE t2.idnane = ? 
                         AND t1.domain = ?
                         AND t1.domain = t2.domain
                         "
    );
    $sth->execute(@cl0);
    my $total_articles_number1 = $sth->fetchrow_array();
    $sth->finish();

    my $sth = $self->{dbh}->prepare(
        "SELECT count(*)
                         FROM mailbox AS t1,
                              domain AS t2
                         WHERE t2.idnane = ? 
                         AND t1.domain = ?
                         AND t1.domain = t2.domain
                         "
    );
    $sth->execute(@cl0);
    my $total_articles_number2 = $sth->fetchrow_array();
    $sth->finish();

    if ( $total_articles_number2 > $total_articles_number1 ) {
        $self->{p}->{total_number} = $total_articles_number2;
    }
    else {
        $self->{p}->{total_number} = $total_articles_number1;
    }

    $self->{p}->{domain_too} = $domains[$#domains];

    if ( !@domains ) {
        $self->{he}->{NOTS3} = 1;
    }
    else {
        $self->{he}->{ROWS3} = \@domains2;
    }

    return $self;
}

sub page_list_mailbox {
    my $self = shift;
    $self->{p}->{file} = 'listvirtual';
    $self->SUPER::page_list(@_);
    return $self;

}

sub return_list_mailbox {
    my $self = shift;

    my @sql = ();
    if (   $self->{session}->{name} eq 'Admin'
        && $self->{p}->{domain}
        && $self->{p}->{username} )
    {
        push @sql, $self->{p}->{domain}, $self->{p}->{username};
    }
    else {
        if ( defined( $self->{p}->{fDomain} ) ) {
            push @sql, $self->{p}->{fDomain}, $self->{session}->{user};
        }
        elsif ( defined( $self->{p}->{domain} ) ) {
            push @sql, $self->{p}->{domain}, $self->{session}->{user};
        }
        else {
            push @sql, $self->{p}->{domain_too}, $self->{session}->{user};
        }
    }

    my $sth = $self->{dbh}->prepare(
        "SELECT t1.domain,
                                t1.username,
                                t1.name,
                                t1.quota,
                                t1.modified,
                                t1.active
                         FROM mailbox AS t1,
                         domain AS t2
                         WHERE t1.domain = ? 
                         AND t2.idnane = ? 
                         
                         AND t1.domain = t2.domain                   
                         ORDER BY t1.username
                         LIMIT ?,?
                             "
    );
    $sth->execute( @sql, $self->{p}->{jj}, $self->{se}->{line_from_page} );

    my @arrayedout = ();
    while ( my $ref = $sth->fetchrow_hashref() ) { push( @arrayedout, $ref ); }
    $sth->finish();

    foreach $_ (@arrayedout) {
        $_->{active} = $_->{active} == 1 ? 'YES' : 'NO';
        $_->{description} ||= 'No Description';
        $_->{username2} = $self->{'p'}->{'username'};
        $_->{from} = $self->{'p'}->{'from'} if ( $self->{'p'}->{'from'} );
        if ( $_->{quota} ne '-1' && $_->{quota} ne '0' ) {
            $_->{quota} = sprintf( '%.1f', $_->{quota} / 1048576 );
        }
    }

    my $sth = $self->{dbh}->prepare(
        'SELECT t1.address,
                                t1.goto,
                                t1.modified, 
                                t1.domain
                         FROM alias AS t1, 
                              domain AS t2
                         WHERE t1.domain = ?
                         AND t2.idnane = ?
                         AND t1.domain = t2.domain                   
                         ORDER BY t1.address
                         LIMIT ?,?
                             '
    );
    $sth->execute( @sql, $self->{p}->{jj}, $self->{se}->{line_from_page} );

    my @arrayedout2 = ();
    while ( my $ref = $sth->fetchrow_hashref() ) { push( @arrayedout2, $ref ); }
    $sth->finish();

    foreach $_ (@arrayedout2) {
        $_->{from} = $self->{p}->{from} if ( $self->{p}->{from} );
        $_->{username2} = $self->{p}->{username};
        $_->{goto} =~ s/,/ /g;
    }

    my $lido;
    if ( $self->{p}->{fDomain} || $self->{he}->{ROWS3} ) {

        my @sqlu = ();
        my $cl;

        if ( defined( $self->{p}->{fDomain} ) ) {
            push @sqlu, $self->{p}->{fDomain}, $self->{p}->{fDomain},
              $self->{p}->{fDomain};
        }
        else {
            push @sqlu, $self->{p}->{domain_too}, $self->{p}->{domain_too},
              $self->{p}->{domain_too};
        }

        if ( $self->{session}->{name} ne 'Admin' ) {
            $cl = 'AND idnane = ?';
            push @sqlu, $self->{session}->{user};
        }

        my $sth = $self->{dbh}->prepare(
            "SELECT aliases,
                                mailboxes,
                               (select count(*) from mailbox where domain = ?) AS countmail,
                               (select count(*) from alias where domain = ?) AS countalias
                         FROM domain
                         WHERE domain = ?
                         $cl
                         LIMIT 1
                         "
        );
        $sth->execute(@sqlu);
        $lido = $sth->fetchrow_hashref();
        $sth->finish();

    }

    $self->{he}->{m_aliases}    = $lido->{aliases};
    $self->{he}->{m_mailboxes}  = $lido->{mailboxes};
    $self->{he}->{m_countmail}  = $lido->{countmail};
    $self->{he}->{m_countalias} = $lido->{countalias};

    $self->{he}->{USERNAME2} = $self->{p}->{username};

    if ( $self->{p}->{fDomain} ne '' ) {
        $self->{he}->{RETURN_DOMAIN} = $self->{'p'}->{fDomain};
    }
    elsif ( $self->{'p'}->{domain} ne '' ) {
        $self->{he}->{RETURN_DOMAIN} = $self->{p}->{domain};
    }
    else {
        $self->{he}->{RETURN_DOMAIN} = $self->{p}->{domain_too};
    }

    if ( !@arrayedout2 ) {
        $self->{he}->{NOTS2} = 1;
    }
    else {
        $self->{he}->{ROWS2} = \@arrayedout2;
    }

    if ( !@arrayedout ) {
        $self->{he}->{NOTS} = 1;
    }
    else {
        $self->{he}->{ROWS} = \@arrayedout;
    }

    return $self;
}

sub templ_hash_mailbox_list {
    my $self = shift;

    $self->{session}->{filet} = 'listmailbox';

    $self->SUPER::templ_hash(@_);
}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
